package net.uman.web.front;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uman.entity.Bbsplate;
import net.uman.entity.PlateUserRelations;
import net.uman.entity.Topic;
import net.uman.entity.User;
import net.uman.framework.modules.orm.Page;
import net.uman.framework.modules.orm.PropertyFilter;
import net.uman.framework.modules.security.springsecurity.SpringSecurityUtils;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.service.BbsplateManager;
import net.uman.service.PlateUserRelationsManager;
import net.uman.service.TopicManager;
import net.uman.util.Constants;
import net.uman.web.CrudActionSupport;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

@Namespace("/front")
public class FrontTopicAction extends CrudActionSupport<Topic>{
	private static final long serialVersionUID = 1L;
	public Long plateId;
	public Long topicId;
	public Bbsplate bbsPlate;
	public Topic topic;
	@Autowired
	private TopicManager topicManager;
	@Autowired
	private PlateUserRelationsManager plateUserRelationsManager;
	@Autowired
	private BbsplateManager bbsplateManager;
	
	public List<PlateUserRelations> plateUserList = new ArrayList<PlateUserRelations>(); 
	
	public Page <Topic> topicPage = new Page<Topic>(15);
	public String topicList(){
		plateUserListInit();
		bbsPlate= bbsplateManager.getBbsplate(plateId);
		return "list";
	}

	/**
	 * 板块的版主初始化列表
	 */
	private void plateUserListInit(){
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter("filter_EQL_bbsPlateId",String.valueOf(plateId)));
		filters.add(new PropertyFilter("filter_EQI_type",String.valueOf(PlateUserRelations.NORMAL)));
		plateUserList = plateUserRelationsManager.getPlateUserRelationsList(filters);
	}
	
	
	public String topicListData(){
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter("filter_EQL_plateId",plateId.toString()));
		filters.add(new PropertyFilter("filter_EQI_type",String.valueOf(Constants.TOPIC_TYPE_ROOT)));
		topicPage.setPageNo(pageNum);
		topicManager.findTopicPage(topicPage,filters);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject jObject = new JSONObject();
		for(Topic topic : topicPage.getResult()){
			jObject.put("id", topic.getId());
			jObject.put("title", topic.getTitle());
			jObject.put("authorName", topic.getAuthorName());
			jObject.put("addDate", topic.getAddDate().toString());
			jObject.put("content", topic.getContent());
			jObject.put("hits", topic.getHits());
			jObject.put("review", topic.getReview());
			jObject.put("plateName", topic.getPlateName());
			jObject.put("status", topic.getStatus());
			jsonArray.add(jObject);
		}
		jsonObject.put("rows", jsonArray);
		jsonObject.put("total", topicPage.getTotalCount());
		Struts2Utils.renderJson(jsonObject);
		return null;
	}
	
	/**
	 * 申请版主
	 * @return
	 */
	public String joinPlateUserRelation(){
		User user = SpringSecurityUtils.getCurrentUser();
		if(user == null){
			Struts2Utils.renderText("noUser");
		}else{
			PlateUserRelations pur= new PlateUserRelations();
			bbsPlate= bbsplateManager.getBbsplate(plateId);
			pur.setBbsPlateId(plateId);
			pur.setType(PlateUserRelations.NEW);
			pur.setUserId(user.getId());
			pur.setUserName(user.getName());
			pur.setBbsPlateName(bbsPlate.getTitle());
			plateUserRelationsManager.save(pur);
			Struts2Utils.renderText("success");
		}
		return null;
	}
	
	/**
	 * 发布新贴
	 * @return
	 */
	public String inputTopic() throws Exception{
		bbsPlate= bbsplateManager.getBbsplate(plateId);
		plateUserListInit();
		return "input";
	}
	
	public Topic getModel() {
		return topic;
	}

	public String  showTopic() throws Exception{
		topic  = topicManager.getTopic(topicId);
		return "showtopic";
	}
	
	@Override
	public String list() throws Exception {
		return null;
	}

	@Override
	public String input() throws Exception {
		return null;
	}

	@Override
	public String save() throws Exception {
		User user  = SpringSecurityUtils.getCurrentUser();
		bbsPlate = bbsplateManager.getBbsplate(plateId);
		topic.setAddDate(new Date());
		topic.setAuthorId(user.getId());
		topic.setAuthorName(user.getName());
		topic.setPlateName(bbsPlate.getTitle());
		topic.setHits(0);
		topic.setReview(0);
		if(topic.getParentId() != null){
			topic.setType(Constants.TOPIC_TYPE_COMMENTS);
		}else{
			topic.setType(Constants.TOPIC_TYPE_ROOT);
		}
		topic.setStatus(Constants.STATUS_NORMAL);
		topicManager.save(topic);
		plateUserListInit();
		bbsPlate= bbsplateManager.getBbsplate(plateId);
		return "list";
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(topicId != null){
			topic = topicManager.getTopic(topicId);
		}else{
			topic = new Topic();
		}
	}

}
